<?php

namespace App\Repositories\Impl;

use App\Models\CultureVoice;
use App\Repositories\Contracts\CultureVoiceRepository;
use App\Repositories\Transformers\CultureVoiceListTransformer;
use App\Repositories\Transformers\CultureVoiceTransformer;

class CultureVoiceRepositoryImpl implements CultureVoiceRepository
{
    public function paginate($perPage, $filter)
    {

        $cultureVoice = new CultureVoice();

        if (isset($filter['type']) && $filter['type'] > 0) {
            $cultureVoice = $cultureVoice->where('type', $filter['type']);
        }

        if (!empty($filter['keyword'])) {
            $cultureVoice = $cultureVoice->where('title', 'like', '%' . $filter['keyword'] . '%')
                ->orWhere('content', 'like', '%' . $filter['keyword'] . '%');
        }

        $cultureVoice = $cultureVoice->orderBy('created_at', 'desc')->paginate($perPage);

        $result = fractal($cultureVoice, new CultureVoiceListTransformer());

        return $result->toArray();

    }

    public function find($id, $columns = ['*'])
    {
        $cultureVoice = CultureVoice::find($id, $columns);

        $result = fractal($cultureVoice, new CultureVoiceTransformer());

        return $result->toArray();

    }

    public function findByFilter($filter, $columns = ['*'])
    {
        return CultureVoice::where($filter)->first($columns);
    }

    public function store($data)
    {
        $cultureVoice = CultureVoice::create($data);

        $result = fractal($cultureVoice, new CultureVoiceTransformer());
        return $result->toArray();

    }

    public function update($data, $id)
    {
        return CultureVoice::whereId($id)->update($data);
    }

    public function updateByFilter($data, $where)
    {
        return CultureVoice::where($where)->update($data);
    }

    public function delete($id)
    {
        return CultureVoice::whereId($id)->delete();
    }
}
